<?php
include 'common/header.html';
include 'common/left.html';
include("common/page.php");
include_once 'module/common/Db.class.php';
use \PFT\Db;
$conf =include 'module/common/db.conf.php';
Db::Conf($conf['remote_1']);
$Db = Db::Connect();

$pageSize=$_REQUEST['pageSize']?$_REQUEST["pageSize"]:10;
$currentPage=$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$offset=($currentPage-1)*$pageSize;

$sql = <<<SQL
SELECT id,activity_name,activity_bt,activity_et,activity_type,buy_limit,
share_page,discount,discount_type,status FROM pft_marketing
WHERE fid=? and status in (0,2) ORDER BY id DESC limit $offset,$pageSize
SQL;
$stmt = $Db->prepare($sql);
$stmt->execute(array($_SESSION['sid']));
$share_pages = array();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
    $rows[]         = $row;
    if ($row['activity_type']==3 && $row['share_page']>0) {
        $share_pages[]  = $row['share_page'];
    }
}
//$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$activity_typeT  =array(1=>'秒杀',2=>'限量打折',3=>'分享立减');
//分享页面数据
if ($cnt = count($share_pages)) {
    $sql = "SELECT id,title,image_path FROM pft_marketing_sp WHERE id IN("
        .implode(',', $share_pages).") LIMIT $cnt";

    $stmt = $Db->prepare($sql);
    $stmt->execute();
    $share_page_list = array();
    while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
        $share_page_list[$row['id']] = $row;
    }
}

$sel_count = "SELECT count(*) from pft_marketing where fid=?  and status in (0,2)";
$stmt = $Db->prepare($sel_count);
$stmt->execute(array($_SESSION['sid']));
$count = $stmt->fetchAll(PDO::FETCH_ASSOC);
$count = $count[0]['count(*)'];

?>
<!--<script type="text/javascript" src="http://www.12301.cc/js/plugin/json2.js"></script>-->
<!--[if lt IE 7]><div id="childListWrap" class="lt-ie10 lt-ie9 lt-ie8 lt-ie7 ie6 childListWrap"> <![endif]-->
<!--[if IE 7]><div id="childListWrap" class="lt-ie10 lt-ie9 lt-ie8 ie7 childListWrap"> <![endif]-->
<!--[if IE 8]><div id="childListWrap" class="lt-ie10 lt-ie9 ie8 childListWrap"> <![endif]-->
<!--[if IE 9]><div id="childListWrap" class="lt-ie10 ie9 childListWrap"><![endif]-->
<!--[if gt IE 9]><!--><div id="childListWrap" class="childListWrap"> <!--<![endif]-->
    <link rel="stylesheet" href="http://www.12301.cc/css/common/www.12301.cc.global.css"/>
    <link rel="stylesheet" href="http://www.12301.cc/css/marketing_list.css"/>
    <link rel="stylesheet" href="css/page.css" media="all" />
<h3 class="mc_title"><span class="mctit_1"><span class="mctit_2">营销活动列表</span></span></h3></br>
    <input type="hidden" id="lid" value="<?=$_REQUEST['lid']?>" />
    <input type="hidden" name="sort" value='<?=json_encode($sort)?>'/>
    <a id="addChildBtn_a" class="addChildBtn" href="marketing_create.html" target="_blank">创建</a>
    <a id="addChildBtn_b" class="addChildBtn" href="marketing_list.html" target="_blank">列表</a>
    <a id="addChildBtn_c" class="addChildBtn" href="marketing_csp.html" target="_blank">分享</a>
    <a id="addChildBtn_d" class="addChildBtn" href="marketing_showlist.html" target="_blank">分享列表</a>
<table id="listTable" class="proTable fixed">
        <thead id="proTableHead">
            <tr>
                <th class="col_1">活动名称</th>
                <th class="col_2">活动时间</th>
                <th class="col_3">活动类型</th>
                <th class="col_3">限购数</th>
                <th class="col_4">分享页面</th>
                <th class="col_4">折扣</th>
                <th class="col_4">状态</th>
                <th class="col_5">操作</th>
            </tr>
        </thead>
        <tbody>
        <?php if(is_array($rows)):?>
            <?php foreach($rows as $k => $v):?>
            <tr class="listBody borderBottom" data-id="<?=$v['id']?>">
                <td class="col_1 col">
                    <span><?=$v['activity_name']?></span>
                </td>
                <td class="col_2 col">
                <span class="numPrice">
                    <?=date('Y-m-d H:i:s',$v['activity_bt'])?></br>
                    <?=date('Y-m-d H:i:s',$v['activity_et'])?></span>
                </td>
                <td class="col_3 col">
                    <span><?=$activity_typeT[$v['activity_type']]?></span>
                </td>
                <td class="col_3 col">
                    <span><?=$v['buy_limit']?></span>
                </td>
                <td>
                    <a data-qr="<?=$share_page_list[$v['share_page']]['image_path']?>"
                       target="_blank"
                       href="javascript:;" class="wx-qr-preview">
                        <?=$share_page_list[$v['share_page']]['title']?>
                    </a>
                </td>
                <td class="col_3 col">
                    <span>
                        <?php
                        if($v['discount_type']==1){
                            echo $v['discount'] / 100 .'折';
                        } else {
                            echo $v['discount'] / 100 . '元';
                        }
                        ?>

                    </span>
                </td>
                <td class="col_4 col">
                    <span class="yujbtn <?=$v['status']=='0'?'onyuj':''?>" id="yuj" data-id="<?=$v['id']?>"></span>
                </td>
                <td class="col_5 col">
<!--                    <a class="do edit" href="marketing_create.html?id=--><?//=$v['id']?><!--" target="_blank"><i class="cpicon edit"></i><i class="cpicon edit-hover hover"></i></a>-->
                    <a class="do close" href="javascript:;"><i class="cpicon close"></i><i class="cpicon close-hover hover"></i></a>
                </td>
            </tr>
            <?php endforeach;?>
        <?php endif;?>
    </tbody>
    </table>
     <?if(count($rows)!=0){page($count,$currentPage,$pageSize);}?>
    <!-- <div class="save_f">
        <!-- <a id="saveToNextBtn" class="saveToNextBtn" href="###">保存</a> 
        </div>
    </div> -->   
<script type="text/javascript" src="http://www.12301.cc/js/global/www.12301.cc.global.js"></script>
<script type="text/javascript" src="http://www.12301.cc/js/marketing/marketing_list.js"></script>
</div>
<script>
    
    
    $("#listTable").on("click",".listBody .yujbtn",function(e){
        var target = $(e.currentTarget);
        var id = target.attr("data-id");
        //alert(id)
        var yucln = e.target.className;
        //alert(yucln) 
        if(yucln == "yujbtn onyuj"){
            $.post('http://www.12301.cc/call/jh_marketing.php',{'action':'changeStatus','marketing_status':'2','marketing_id':+id}).done(function(code){
                if(code=='200'){
                //alert(target)
                //alert(111)
                    e.target.className = "yujbtn";
                }
            }).error(function(){
                alert('发生错误，无法改变状态');
            });
            
        }else{
            $.post('http://www.12301.cc/call/jh_marketing.php',{'action':'changeStatus','marketing_status':'0','marketing_id':+id}).done(function(code){
                if(code=='200'){
                   // document.getElementById('yuj').className = "yujbtn onyuj";
                   //alert(111)
                   target.addClass("onyuj");
                } else {
                    alert(response);
                }
            }).error(function(){
                alert('发生错误，无法改变状态');
            }); 
    }
    })
    //创建营销活动
    $("#addChildBtn").on("click",function(e){
            var lid = $("#lid").val();
            window.location.href = "http://www.12301.cc/markteing_create.html?lid="+lid;
            return false;
        })
    $("#listTable").on("click",".listBody .close",function(e){ //删除
            var target = $(e.currentTarget);
            var result = window.confirm("确定要删除此景点吗");
            if(!result) return false;
            var citem = target.parents(".listBody");
            deleteChild(citem.data("id"),citem);
        })

    function deleteChild(id,citem){ 
        //if(!id) return false;
        PFT_GLOBAL.G.Ajax({
            url : "http://www.12301.cc/call/jh_marketing.php",
            type : "POST",
            dataType : "JSON",
            data : {
                action : "changeStatus",
                marketing_id : id,
                marketing_status:"3"
            },
            loading : function(){

            },
            removeLoading : function(){

            }
        },function(code){
            if(code == '200'){
                citem.remove();
                restor();
                PFT_GLOBAL.U.Alert("success",'<p style="width:160px">删除成功</p>',1.5 * 1000);
            }else{
                PFT_GLOBAL.U.Alert("fail",'<p style="width:160px">删除失败，请稍后重试</p>',1.5 * 1000);
            }
        })
    }
</script>
<?php include('common/footer.html')?>